import os
import datetime
from PIL import Image
from core.wchat.img import make_qr_img
from core.tools.edit_photo import EditPicture
from finance_c2c.local_settings import Config


class PopularUtils(EditPicture):
    @classmethod
    def create(cls, salesman, popular_obj, qr_code_path):
        img_path = popular_obj.img.url  # 上传的推广图
        img_path = cls.get_admin_absolute_path(img_path)

        base_save_path = os.path.join(Config.base_dir, "c2c_media", "popular/hc/")
        cls.make_dir(base_save_path)
        file_name = "%s-%s.jpg" % (popular_obj.pk, salesman.pk)
        save_path = os.path.join(base_save_path, file_name)  # 需要保存的位置

        bottom_img, bottom_buf = cls.open_rb_to_img(img_path)
        cover_img, cover_buf = cls.open_rb_to_img(qr_code_path)

        layer = Image.new('RGBA', bottom_img.size, (0, 0, 0, 0))
        for i in popular_obj.pasteinfo_set.all():
            cover_img = cover_img.resize(eval(i.paste_size), Image.ANTIALIAS)  # 变化大小
            layer.paste(cover_img, eval(i.position))  # 贴图片
            bottom_img = Image.composite(layer, bottom_img, layer)
        font_file = os.path.join(Config.base_dir, "core/font_file/msyh.ttc")

        # 这里文字
        words = []
        for i in popular_obj.pasteinfowords_set.all():
            d = dict()
            d["font_file"] = font_file
            d["font_size"] = i.font_size
            d["box"] = eval(i.position)
            d["color"] = eval(i.color)
            if i.words_type == 1:  # 手机号
                d["words"] = salesman.tel
            else:
                d["words"] = salesman.expire_time.strftime("%Y-%m-%d")
            words.append(d)
        cls.write_words_return(bottom_img, words)
        bottom_img.save(save_path)
        bottom_buf.close()
        cover_buf.close()
        return save_path

    @classmethod
    def check_qrcode(cls, salesman):
        now_time = datetime.datetime.now()
        if salesman.expire_time and salesman.expire_time > now_time:
            qr_code_path = salesman.scene_qr_code
            qr_code_path = os.path.join(Config.base_dir, qr_code_path[1:])
            status = True
        else:
            # 二维码已过期， 重新生成二维码
            qr_code_path = make_qr_img(salesman, short=False)  # 获取到二维码的地址
            status = False
        return qr_code_path, status

    @classmethod
    def create_channel_popular(cls, img_path, file_name, qr_code_path, catalog="popular/cp/", qrcode_box=(160, 160),
                               position=(100, 100)):
        """
        创建渠道推广图(我的里)
        :param file_name: 文件名字
        :param img_path: 底部文件绝对路径
        :param qr_code_path: 二维码绝对路径
        :param catalog: 相对media的路径 "popular/cp/"  channel popular
        :param qrcode_box: 二维码尺寸
        :param position: 二维码贴的位置
        :return:
        """
        base_save_path = os.path.join(Config.base_dir, "c2c_media", catalog)
        cls.make_dir(base_save_path)
        save_path = os.path.join(base_save_path, file_name)  # 需要保存的位置
        img_path = cls.get_admin_absolute_path(img_path)
        bottom_img, bottom_buf = cls.open_rb_to_img(img_path)
        print(qr_code_path, 'ddd')
        cover_img, cover_buf = cls.open_rb_to_img(qr_code_path)
        layer = Image.new('RGBA', bottom_img.size, (0, 0, 0, 0))
        cover_img = cover_img.resize(qrcode_box, Image.ANTIALIAS)  # 变化大小
        layer.paste(cover_img, position)  # 贴图片
        bottom_img = Image.composite(layer, bottom_img, layer)
        bottom_img.save(save_path)
        return save_path

    @classmethod
    def get_admin_absolute_path(cls, img_path):
        """
        获取django admin上传的图片的额绝对路径
        :param img_path:
        :return:
        """
        return os.path.join(Config.base_dir, img_path[1:])  # 绝对路径
